package cn.znzz.cjm.basic.controller;

import cn.znzz.cjm.basic.dto.LoginDto;
import cn.znzz.cjm.basic.exception.BusinessException;
import cn.znzz.cjm.basic.service.ILoginService;
import cn.znzz.cjm.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/*
登录接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;
    //账号密码登录
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    //微信登录
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> params){
        try {
            return loginService.wechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    //微信绑定
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> params){
        try {
            Map<String, Object> map = loginService.binder(params);
            System.out.println(map);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}















